package com.sunnada.edu.business.salaryManage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.business.attendance.bean.AttenRecord;
import com.sunnada.edu.business.attendance.bean.Attendance;
import com.sunnada.edu.business.salaryManage.service.ExitSettlementService;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;

/***
 * explain 退场结算
 * 
 * @author zhenglx
 */
@Controller
@RequestMapping("/exitSettlementController")
public class ExitSettlementController extends BasicController {
	@Resource
	ExitSettlementService exitSettlementService;

	/** 退场结算首页 */
	@RequestMapping("/exitSettlement")
	public String exitSettlement(HttpServletRequest request) {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "business/salaryManage/exitSettlement";
	}

	/** 退场结算查询 */
	@RequestMapping("/exitSettlementSearch")
	public void exitSettlementSearch(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		if(params.get("workerId") != null){
			params.put("workerId", Utils.getIdsWithSingleQuotes(params.get("workerId")));
		}
		
		Page page = new Page();
		page.setRowCount(exitSettlementService.getExitSettlementCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = exitSettlementService.findExitSettlement(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 考勤记录修改页面 */
	@RequestMapping("/attenRecordUpdate")
	public String attendanceUpdate(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		return "business/attendance/attendance_update";
	}
	
	/** 考勤记录保存功能 */
	@RequestMapping("/attenRecordSave")
	public void userSave(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		// 验证用户账号是否重复
		String recId = params.get("recId");
		if (Utils.isNotNull(recId)) {
			msg = exitSettlementService.attendanceUpdate(params);
		} else {
			recId = UUID.randomUUID().toString();
			params.put("recId", recId);
			msg = exitSettlementService.attenRecordAdd(params);
		}
		printInfo(response, msg);
	}

	/** 工资表导出 */
	@RequestMapping("/exportAttendance")
	public void exportAttendance(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, AttenRecord.class, params);
		try {
			exitSettlementService.exportAttendance(params, request, response);
		} catch (Exception e) {
			printInfo(response, "<script>alert('导出失败，请稍后重试！');window.history.go(-1);</script>");
		}
	}
	
	/** 结算工资审核提交 */
	@RequestMapping("/exitpayApply")
	public void exitpayApply(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		msg = exitSettlementService.exitpayApply(params);
		printInfo(response, msg);
	}
	
	/*
	 * 根据申请单ID获取申请单信息
	 */
	@RequestMapping("/getExitPayApplyByRecid")
	public void getExitPayApplyByRecid(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		List<?> list = exitSettlementService.getExitPayApplyByRecid(params);
		JSONObject json = new JSONObject();		
		json.put("list", list);
		printInfo(response,json);
	}
	
	/** 申请单对应工人查询 */
	@RequestMapping("/getExitPayApplyWorker")
	public void getExitPayApplyWorker(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		Page page = new Page();
		page.setRowCount(exitSettlementService.getExitPayApplyWorkerCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = exitSettlementService.findExitPayApplyWorker(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}
	
	/**退场结算申请审核*/
	@RequestMapping("/audit")
	public void audit(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		msg = exitSettlementService.audit(params);		
		printInfo(response,msg);
	}
	
	/** 跳转退场就算页面 */
	@RequestMapping("/pageEditExitSettlement")
	public String pageEditSalary(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		request.setAttribute("workerId", params.get("workerId"));
		request.setAttribute("userName", params.get("optionUserName"));
		return "business/salaryManage/edit_exitSettlement";
	}
}
